package fr.univlille1.ftp;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author louis
 */
public class Transferer extends Thread{

    private boolean inOut;
    private String filePath;
    private FTPClient client;
    private File file;
    
    public Transferer(boolean inOut, String filePath, FTPClient client, File file){
        super();
        this.inOut = inOut;
        this.filePath = filePath;
        this.client = client;
        this.file = file;
    }
    
    @Override
    public void run() {
        try {
            if(this.inOut == true){
                this.download();
            }else if(this.inOut == false){
                this.upload();
            }
            this.client.disconnect(true);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPIllegalReplyException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void upload(){
        try {
            this.client.upload(this.filePath, new FileInputStream(this.file), 0, 0, null);
        }  catch (IllegalStateException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPIllegalReplyException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPDataTransferException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPAbortedException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
    
    private void download(){
        try {
            this.client.download(this.filePath, this.file, 0, null);
        } catch (IllegalStateException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPIllegalReplyException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPDataTransferException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPAbortedException ex) {
            Logger.getLogger(Transferer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
